﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Adver;

namespace Ue.Qcdz.Repositories.Adver
{
    public class AdvertisementRepository : SqlServerRepository<Advertisement>, IAdvertisementRepository
    {
        public AdvertisementRepository()
            : this(null) { }

        public AdvertisementRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork)
        {

        }

        #region Overrides of SqlServerRepositoryBase<Advertisement>

        protected override string GetDataTableName()
        {
            return "Advertisement";
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                           AdvertisementFactory.FiledNames.EntityKey,
                           AdvertisementFactory.FiledNames.WebSiteName,
                           AdvertisementFactory.FiledNames.DivId,
                           AdvertisementFactory.FiledNames.AdType,
                           AdvertisementFactory.FiledNames.AdSize,
                           AdvertisementFactory.FiledNames.AdUrl,
                           AdvertisementFactory.FiledNames.AdLink,
                           AdvertisementFactory.FiledNames.PageName,
                           AdvertisementFactory.FiledNames.Remark
        };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                           AdvertisementFactory.FiledNames.WebSiteName,
                           AdvertisementFactory.FiledNames.DivId,
                           AdvertisementFactory.FiledNames.AdType,
                           AdvertisementFactory.FiledNames.AdSize,
                           AdvertisementFactory.FiledNames.AdUrl,
                           AdvertisementFactory.FiledNames.AdLink,
                           AdvertisementFactory.FiledNames.PageName,
                           AdvertisementFactory.FiledNames.Remark
                       };
        }

        protected override IDataParameter[] BuildParameters(Advertisement entity)
        {
            return new IDataParameter[]
                       {
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.EntityKey,entity.Key),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.WebSiteName,entity.WebSiteName),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.DivId,entity.DivId),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.AdType,entity.AdType),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.AdSize,entity.AdSize),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.AdUrl,entity.AdUrl),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.AdLink,entity.AdLink),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.PageName,entity.PageName),
                           this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.Remark,entity.Remark)
                       };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            var advertisement = dataQuery as UI.Controls.Adver.Advertisement;
            if (advertisement != null)
            {

                return new AdWhereClause(advertisement);

            }
            return null;
        }
        public class AdWhereClause : IWhereClause
        {
            private string strWebname = "";
            private string strPageName = "";
            private string strDivId = "";
            private Ue.Qcdz.UI.Controls.Adver.Advertisement advertisement;
            public AdWhereClause(Ue.Qcdz.UI.Controls.Adver.Advertisement advertisement)
            {
                this.advertisement = advertisement;
                strWebname = advertisement.WebSiteName;
                if (string.IsNullOrEmpty(strWebname)) strWebname = "";
                strPageName = advertisement.PageName;
                if (string.IsNullOrEmpty(strPageName)) strPageName = "";
                strDivId = advertisement.DivId;
                if (string.IsNullOrEmpty(strDivId)) strDivId = "";
            }

            #region Implementation of IWhereClause

            public string Clause
            {
                get
                {
                    var clause =
                        "WebSiteName LIKE '%'+@WebSiteName+'%' AND PageName LIKE '%'+@PageName+'%' AND DivId LIKE '%'+@DivId+'%'";
                    return clause;
                }
            }

            public IDataParameter[] Parameters
            {
                get
                {
                    var Parameters = new IDataParameter[]
                                        {
                                        SqlServerDriver.Instance.BuildParameter(AdvertisementFactory.FiledNames.WebSiteName,
                                                                                strWebname),    
                                        SqlServerDriver.Instance.BuildParameter(AdvertisementFactory.FiledNames.PageName,
                                                                                 strPageName),    
                                        SqlServerDriver.Instance.BuildParameter(AdvertisementFactory.FiledNames.DivId,
                                                                                 strDivId)
                                        };
                    return Parameters;
                }
            }

            #endregion
        }
        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }
        protected override string GetOrderBy()
        {
            return "ID DESC";
        }
        protected override string GetKeyFieldName()
        {
            return AdvertisementFactory.FiledNames.EntityKey;
        }
        #endregion

        #region Implementation of IAdvertisementRepository

        public List<Advertisement> FindByWeb(string webSiteName, string pageName, string divId)
        {
            string sql =
                "SELECT * FROM Advertisement WHERE WebSiteName LIKE '%@WebSiteName%' AND PageName LIKE '%@PageName%' AND DivId LIKE '%@DivId%'";
            var parameter = new IDataParameter[]
                                {
                                     this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.WebSiteName,
                                                                                      webSiteName),
                                    this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.PageName,
                                                                                      pageName),
                                    this.RepositoryDriver.BuildParameter(AdvertisementFactory.FiledNames.DivId,
                                                                                      divId),    
                                };
            return (List<Advertisement>)BuildEntitiesFromSql(sql, parameter);

        }

        #endregion

        #region Implementation of IAdvertisementRepository

        public Advertisement FindBy(string webSiteName, string pageName, string strdivId)
        {
            var query = "SELECT * FROM Advertisement WHERE WebSiteName=@WebSiteName AND PageName=@PageName AND DivId=@DivId";
            var parameter = new IDataParameter[]
                                             {
                                                 this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.WebSiteName,
                                                                                      webSiteName),
                                                 this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.PageName,
                                                                                      pageName),
                                                this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.DivId,
                                                strdivId)      
            };
            return BuildEntityFromSql(query, parameter);
        }

        public IList<Advertisement> Find(string webSiteName, string pageName, string strdivId)
        {
            var query = "SELECT * FROM Advertisement WHERE WebSiteName=@WebSiteName AND PageName=@PageName AND DivId=@DivId";
            var parameter = new IDataParameter[]
                                             {
                                                 this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.WebSiteName,
                                                                                      webSiteName),
                                                 this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.PageName,
                                                                                      pageName),
                                                this.RepositoryDriver.BuildParameter( AdvertisementFactory.FiledNames.DivId,
                                                strdivId)      
            };
            return this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
